﻿Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Imports AugmentLib.InputOutput.Encryption
Partial Class Dashboard
    Inherits System.Web.UI.Page

#Region " Properties "

    Public Property CurrentPage() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPage As Object = ViewState("_CurrentPage")
            If objPage Is Nothing Then
                Return 0
            Else
                'default to showing the first page
                Return CInt(objPage)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_CurrentPage") = value
        End Set
    End Property

    Public Property PageSize() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPageSize As Object = ViewState("_PageSize")
            If objPageSize Is Nothing Then
                Return 10
            Else
                'default to showing the first page
                Return CInt(objPageSize)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_PageSize") = value
        End Set
    End Property

#End Region

#Region " Paging and Page Size "

    Protected Sub lnkHead10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead10Item.Click
        PageSize = 10
        loadAnnouncement()
    End Sub

    Protected Sub lnkHead25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead25Item.Click
        PageSize = 25
        loadAnnouncement()
    End Sub

    Protected Sub lnkHead100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead100Item.Click
        PageSize = 100
        loadAnnouncement()
    End Sub

    Protected Sub lnkHead150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead150Item.Click
        PageSize = 150
        loadAnnouncement()
    End Sub

    Protected Sub lnkFoot10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot10Item.Click
        PageSize = 10
        loadAnnouncement()
    End Sub

    Protected Sub lnkFoot25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot25Item.Click
        PageSize = 25
        loadAnnouncement()
    End Sub

    Protected Sub lnkFoot100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot100Item.Click
        PageSize = 100
        loadAnnouncement()
    End Sub

    Protected Sub lnkFoot150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot150Item.Click
        PageSize = 150
        loadAnnouncement()
    End Sub

    Protected Sub btnMoveFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveFirst.Click
        CurrentPage = 0
        txtPageNo.Text = CurrentPage + 1
        loadAnnouncement()
    End Sub

    Protected Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        If CurrentPage > 0 Then
            CurrentPage = CurrentPage - 1
            txtPageNo.Text = CurrentPage + 1
            loadAnnouncement()
        End If
    End Sub

    Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click
        If CInt(txtPageNo.Text < 1) Then
            txtPageNo.Text = 1
        End If

        If CInt(txtPageNo.Text > CInt(lblMaxPageCount.Text)) Then
            txtPageNo.Text = CInt(lblMaxPageCount.Text) + 1
        End If

        CurrentPage = CInt(txtPageNo.Text) - 1
        loadAnnouncement()

    End Sub

    Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If CurrentPage < CInt(lblMaxPageCount.Text) Then
            CurrentPage = CurrentPage + 1
            txtPageNo.Text = CurrentPage + 1
            loadAnnouncement()
        End If
    End Sub

    Protected Sub btnMoveLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveLast.Click
        CurrentPage = CInt(lblMaxPageCount.Text)
        txtPageNo.Text = CurrentPage + 1
        loadAnnouncement()
    End Sub

#End Region

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("StaffId") Is Nothing Then
            Response.Redirect("Default.aspx")
        Else
            lblStaffUID.Text = Session("StaffId").ToString
            lblStaffType.Text = Session("StaffType").ToString
        End If

        If Not Page.IsPostBack Then
            loadAnnouncement()
            Dim intReminder As Integer = 0
            intReminder = GetReminderCount()
            If intReminder > 0 Then
                lnkReminder.Text = "You have " & intReminder & " reminder!"
            Else
                lnkReminder.Text = "No new reminder"
            End If
        End If
    End Sub

    Private Function GetReminderCount() As Integer
        Dim intReminderCount As Integer
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable
        Try
            objDB.OpenConnection()
            objDB.dbVariable.SQLString = "SELECT RenId, CreateDate FROM AW_Reminder WHERE RenId=@RenId AND StatusDelete=0"
            objDB.AddParameter("RenId", Data.SqlDbType.UniqueIdentifier, New Guid(lblStaffUID.Text))
            objDB.FillData(TempDT, "AW_Reminder", Database.SQLCommandType.NormalString)

            objDB.CloseConnection()
        Catch ex As Exception

        Finally
            If (objDB IsNot Nothing) Then
                objDB.CloseConnection()
            End If
        End Try
        'to filter those more than Active Reminder Interval
        Dim intReminderDays As Integer = getReminderDays()
        Dim dtView As Data.DataView = New Data.DataView
        With dtView
            .Table = TempDT
            '.RowFilter = "CreateDate >=#" & Now.AddDays(-intReminderDays) & "#"
        End With

        TempDT = dtView.ToTable

        If TempDT.Rows.Count > 0 Then
            intReminderCount = TempDT.Rows.Count
        End If
        Return intReminderCount
    End Function

    Private Function getReminderDays() As Integer
        Dim intReminderDays As Integer = 0
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable
        Try
            objDB.OpenConnection()
            objDB.dbVariable.SQLString = "SELECT IntActiveReminder FROM AW_Setting"
            objDB.FillData(TempDT, "AW_Setting", Database.SQLCommandType.NormalString)
            objDB.CloseConnection()

            If TempDT.Rows.Count > 0 Then
                intReminderDays = CInt(TempDT.Rows(0).Item("IntActiveReminder").ToString)
            End If
        Catch ex As Exception

        Finally
            If (objDB IsNot Nothing) Then
                objDB.CloseConnection()
            End If
        End Try
        Return intReminderDays
    End Function

    Private Sub loadAnnouncement()
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable
        Try
            objDB.OpenConnection()

            objDB.dbVariable.SQLString = "SELECT Title, CASE Priority WHEN 1 THEN 'HIGH' WHEN 2 THEN 'MEDIUM' ELSE 'LOW' END as Priority, " & _
                                        "a.AnnouncementId, c.name as CreateBy, a.CreateDate FROM AW_Announcement a " & _
                                        "LEFT JOIN AW_AnnouncementToList b ON a.AnnouncementId = b.AnnouncementId " & _
                                        "LEFT JOIN AW_Staff c ON c.StaffId = b.StaffId " & _
                                        "WHERE b.StaffId = @StaffId AND a.StatusDelete = 0"
            objDB.AddParameter("StaffId", Data.SqlDbType.UniqueIdentifier, New Guid(lblStaffUID.Text.Trim))
            objDB.FillData(TempDT, "AW_Announcement", Database.SQLCommandType.NormalString)

            objDB.CloseConnection()
        Catch ex As Exception

        Finally
            If (objDB IsNot Nothing) Then
                objDB.CloseConnection()
            End If
        End Try
        'to filter those more than Active Reminder Interval
        Dim intReminderDays As Integer = getAnnouncementDays()
        Dim dtView As Data.DataView = New Data.DataView
        With dtView
            .Table = TempDT
            '.RowFilter = "CreateDate >=#" & Now.AddDays(-intReminderDays) & "#"
        End With

        TempDT = dtView.ToTable

        'Creating a Paged Datasouce
        Dim objPaged As New PagedDataSource
        objPaged.DataSource = dtView
        objPaged.AllowPaging = True
        objPaged.PageSize = PageSize
        objPaged.CurrentPageIndex = CurrentPage

        lblHeadNoOfItem.Text = TempDT.Rows.Count
        lblFootNoOfItem.Text = TempDT.Rows.Count

        repAnnouncement.DataSource = objPaged
        repAnnouncement.DataBind()

        lblMaxPageCount.Text = objPaged.PageCount - 1
        lblTotalPage.Text = " of " & objPaged.PageCount
    End Sub

    Private Function getAnnouncementDays() As Integer
        Dim IntActiveAnnouncement As Integer = 0
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable
        Try
            objDB.OpenConnection()
            objDB.dbVariable.SQLString = "SELECT IntActiveAnnouncement FROM AW_Setting"
            objDB.FillData(TempDT, "AW_Setting", Database.SQLCommandType.NormalString)
            objDB.CloseConnection()
        Catch ex As Exception

        Finally
            If (objDB IsNot Nothing) Then
                objDB.CloseConnection()
            End If
        End Try
        If TempDT.Rows.Count > 0 Then
            IntActiveAnnouncement = CInt(TempDT.Rows(0).Item("IntActiveAnnouncement").ToString)
        End If

        Return IntActiveAnnouncement
    End Function

    Protected Sub repAnnouncement_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles repAnnouncement.ItemCommand
        If e.CommandName = "View" Then
            Me.Session.Add("id", e.CommandArgument)
            Response.Redirect("AnnouncementView.aspx")
        End If
    End Sub

    Protected Sub lnkReminder_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkReminder.Click
        Response.Redirect("Reminder.aspx")
    End Sub
End Class
